package com.lp.RSTX.controller;


import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Slf4j
@Controller
@Configuration
@RequestMapping("/user")
public class Unzip {


    @PostMapping(value = "/unzip")
    @ResponseBody
    public String uploadFile(@ApiParam(value = "上传视频文件") MultipartFile jarFile, String name) {

        try {
            if (jarFile.getOriginalFilename() == null) {
                log.info(name + "上传文件为空，请重新上传\n");
            } else {

                as(jarFile, name);


            }
        } catch (NullPointerException e) {
            log.info(name + "上传文件为空，请重新上传\n");

        }
        return "200";
    }

    public static void as(MultipartFile jarFile, String name) {
        log.info("进入方法");
        String path = "/data/录屏记录/" + name + "/";
        File file = new File(path);
        file.setWritable(true, false);
        log.info("上传人是："+file.getName());
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        if(!file.exists()) {
            boolean mkdir = file.mkdirs();
            log.info("文件夹创建"+mkdir);
        }
        log.info("文件名称是" + jarFile.getOriginalFilename());
        try {
            File dest = new File(new File(path).getAbsolutePath() + "/" + jarFile.getOriginalFilename());
            jarFile.transferTo(dest);
            log.info("上传成功，文件放在" + path + "路径下");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}